const app = getApp();
const { AskPage } = app._require("common/index");
AskPage({
    data: {
        navBarHeight: app.globalData.navBarHeight,//导航栏高度
        total: 0,
        tableData: [],
        pageIndex: 1,
        pid: '',
        id: '',
        field: '',
        pname: '',
        name: '',
        show: false,
        key: 0,
        // 用于显示的数组对象
        objectMultiShow:[],
        // 所有显示内容
        objectMultiArray:[],
        // 选项列表（用于显示的）
        multiArray: [],
        multiIndex: [],
        inputValue: '',
        pages: 0
    },
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: options.pname + '-' + options.name
        })
        this.setData({
            pid : options.pid,
            id: options.id,
            field: options.field,
            pname: options.id,
            name: options.id
        });
    },
    onPullDownRefresh() {
        this.setData({
            pageIndex: 1
        });
        this.getData();
    },
    onReachBottom(){
        if (this.data.pageIndex < this.data.pages) {
            this.setData({
                pageIndex: this.data.pageIndex + 1
            });
            this.getData();
        }
    },
    onShow: function () {
        this.getType();
    },
    bindKeyInput: function (e) {
        this.setData({
            inputValue: e.detail.value
        })
    },
    async getData(e) {
        if (e && e.detail && e.detail.type === 'search') {
            this.setData({
                pageIndex: 1
            });
        }
        let arr = []
        if (this.data.multiIndex.length) {
            arr = [{
                field: this.data.objectMultiShow[1][this.data.multiIndex[1]].field,
                term: "5",
                value: this.data.inputValue
            }]
        }
        const response = await this.$_server("searchData/list", {
            data: {
                currentPage: 1,
                pageSize: this.data.pageIndex * 10,
                typeId: this.data.id,
                searchConditionVoList: arr,
                cityCode: app.user.userInfo.cityCode
            }
        });
        let { data } = response;
        const typeList = this.data.objectMultiArray[1]
        const _typeList = typeList.filter(item => !['5', '8', '9', '10'].includes(item.valueKindId)).slice(0, 3);
        const tableData = data.data.records.map(item => {
            const columnList = []
            _typeList.forEach(it => {
                columnList.push({
                    field: it.field,
                    name: it.fieldName,
                    value: item[it.field]
                })
            })
            item.columnList = columnList
            if (this.data.multiIndex.length === 2) {
                item.searchField = {
                    field: this.data.objectMultiShow[1][this.data.multiIndex[1]].field,
                    name: this.data.multiArray[1][this.data.multiIndex[1]],
                    value: item[this.data.objectMultiShow[1][this.data.multiIndex[1]].field]
                }
            }
            return item
        })
        try {
            this.setData({
                tableData: tableData,
                total: data.data.total,
                pages: data.data.pages
            });
        } catch (e) {}
    },
    async getType() {
        const response = await this.$_server("searchData/getType", {
            data: {}
        });
        const parr = response.data.data.filter((item) => item.id === this.data.pid);
        let arr = parr[0].children.filter((item) => item.id === this.data.id);
        // 日后需要级联，就注释这坨
        arr.forEach(item => {
            item.children = item.children.filter(it => it.field === 'basic')
        })

        let data = {
            objectMultiShow: this.data.objectMultiShow,
            objectMultiArray: this.data.objectMultiArray,
            multiArray: this.data.multiArray,
            multiIndex: this.data.multiIndex,
        }

        const flatten = (arr) => { return [].concat(...arr.map(x => Array.isArray(x) ? flatten(x) : x)) }
        const firstArr = flatten(arr.map(item => item.children));
        const towArr = [];
        firstArr.forEach(item => {
            item.labelList.forEach(it => {
                if (!["5", "8", "9", "10"].includes(it.valueKindId)) {
                    towArr.push({
                        ...it,
                        name: it.fieldName,
                        parentId: item.id
                    })
                }
            })
        })
        data.objectMultiArray = [
            firstArr,
            towArr
        ]
        data.objectMultiShow = data.objectMultiArray.map((item, index) => {
            if (index > 0) {
                item = item.filter(i => i.parentId === data.objectMultiArray[index - 1][0].id)
            }
            return item
        })
        data.multiArray = data.objectMultiShow.map(item => {
            item = item.map(i => i.name)
            return item
        })
        // 数据更新
        this.setData(data)
        this.getData();
    },
    bindMultiPickerChange(e) {
        if (e.type === 'change') {
            this.setData({
                multiIndex: e.detail.value
            })
        }
    },
    bindMultiPickerColumnChange(e) {
        const { column, value } = e.detail
        const data = {
            objectMultiShow: this.data.objectMultiShow,
            multiArray: this.data.multiArray,
            multiIndex: this.data.multiIndex
        };

        const id = data.objectMultiShow[column][value].id

        const allList = this.data.objectMultiArray
        if (column === 0) {
            data.multiIndex = [value, 0]
            data.objectMultiShow = [data.objectMultiShow[0], allList[1].filter(item => item.parentId === id)]
            data.multiArray = [data.multiArray[0], data.objectMultiShow[1].map(item => item.name)]
        } else {
            data.multiIndex = [data.multiIndex[0], value]
        }

        this.setData(data);
    },
    toEdit(e) {
        const { item } = e.currentTarget.dataset
        wx.navigateTo({
            url: `/pages/resource-search/edit/index?currentId=${this.data.id}&currentFiled=${this.data.field}&id=${item.id}&pname=${this.data.pname}&name=${this.data.name}`
        })
    },
    toAdd() {
        wx.navigateTo({
            url: `/pages/resource-search/edit/index?currentId=${this.data.id}&currentFiled=${this.data.field}&pname=${this.data.pname}&name=${this.data.name}`
        })
    }
});
